//
//  HFStatusView.m
//  HFTV
//
//  Created by Mr.Zhang on 2017/5/23.
//  Copyright © 2017年 Mr.Zhang. All rights reserved.
//

#import "HFStatusView.h"

#define hfMessageCountLabelHeight  (HFIphoneX ? (HFNavigationHeight == 44 ? 88 : HFNavigationHeight) : 74)
#define hfMessageHorizontalHeight  (HFIphoneX && !isHorizontalScreen ? 74 : 20)
#define hfStatus_isInLiveRoom      [[[NSUserDefaults standardUserDefaults]objectForKey:HFIsInLiveRoom]isEqualToString:@"1"]
#define hfStatus_isInCreateLiveRoom [[[NSUserDefaults standardUserDefaults]objectForKey:HFIsInCreateLiveRoom]isEqualToString:@"1"]

@interface  HFStatusView()

@property (nonatomic,strong) UILabel      *messageLabel;
@property (nonatomic,strong) UIImageView  *statusImageView;

@property (nonatomic,copy)   NSString     *message;
@property (nonatomic,assign) HFStatusType type;
@property (nonatomic,assign) BOOL         statusBarHidden;
@end

@implementation HFStatusView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        _messageLabel = [[UILabel alloc] init];
        _messageLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_messageLabel];
        
        _statusImageView = [[UIImageView alloc]init];
        _statusImageView.width = 20;
        _statusImageView.height = 20;
        [self addSubview:_statusImageView];
    }
    return self;
}

#pragma mark - 显示提示框无回调
+(instancetype)showSuccessMessage:(NSString *)title{
    if (!ValidString(title)) title = @"未知异常";
    return [self showMessageWithTitle:title type:HFStatusType_Success statusBarHidden:NO];
}
+(instancetype)showFailureMessage:(NSString *)title{
    if (!ValidString(title)) title = @"未知异常";
    return [self showMessageWithTitle:title type:HFStatusType_Failure statusBarHidden:NO];
}

+(instancetype)showMessageWithTitle:(NSString *)title
                               type:(HFStatusType)type
                    statusBarHidden:(BOOL)hidden
{
    
    HFStatusView *statusView = [[HFStatusView alloc]init];
    statusView.backgroundColor =  [UIColor whiteColor];
    statusView.x = 0;
    statusView.y = -hfMessageCountLabelHeight;
    statusView.width = FullScreenWidth;
    statusView.height = hfMessageCountLabelHeight;
    statusView.type = type;
    statusView.message = title;
    statusView.statusBarHidden = hidden;
    [[UIApplication sharedApplication].keyWindow addSubview:statusView];
    
    CGFloat duration = 0.32;
    statusView.alpha = 0.0;
        
        [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:.6 initialSpringVelocity:.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            statusView.alpha = 1.0;
            statusView.transform = CGAffineTransformMakeTranslation(0, hfMessageCountLabelHeight  - (HFIphoneX ? 0 : 10));
        } completion:^(BOOL finished) {
            
            CGFloat delay = 2.0;
            [[UIApplication sharedApplication].keyWindow bringSubviewToFront:statusView];
            
            [UIView animateWithDuration:0.1 delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:^{
                
                statusView.alpha = 0.0;
                statusView.transform = CGAffineTransformIdentity;
                
            } completion:^(BOOL finished) {
                
                [[UIApplication sharedApplication].keyWindow bringSubviewToFront:statusView];
                [statusView removeFromSuperview];
                
                if (![statusView isInWindow]) {
                    statusView.statusBarHidden = NO;
                }
            }];
        }];
    return statusView;
}

#pragma mark - 显示提示框有回调
+(instancetype)showMessageWithTitle:(NSString *)title
                               type:(HFStatusType)type
                    statusBarHidden:(BOOL)hidden
                        finishBlock:(StatusFinishBlock)block
{
    HFStatusView *statusView = [[HFStatusView alloc]init];
    statusView.backgroundColor =  [UIColor whiteColor];
    statusView.x = 0;
    statusView.y = -hfMessageCountLabelHeight;
    statusView.width = FullScreenWidth;
    statusView.height = hfMessageCountLabelHeight;
    statusView.type = type;
    statusView.message = title;
    statusView.statusBarHidden = hidden;
    statusView.statusBlock = block;
    
    [[UIApplication sharedApplication].keyWindow addSubview:statusView];
    
    CGFloat duration = 0.32;
    statusView.alpha = 0.0;
    
    [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:.6 initialSpringVelocity:.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        statusView.alpha = 1.0;
        
        statusView.transform = CGAffineTransformMakeTranslation(0, hfMessageCountLabelHeight - (HFIphoneX ? 0 : 10));
        
    } completion:^(BOOL finished) {
        
        CGFloat delay = 1.0;
        
        [[UIApplication sharedApplication].keyWindow bringSubviewToFront:statusView];
        
        [UIView animateWithDuration:0.1 delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:^{
            
            statusView.alpha = 0.0;
            
            statusView.transform = CGAffineTransformIdentity;
            
        } completion:^(BOOL finished) {
            
            [[UIApplication sharedApplication].keyWindow bringSubviewToFront:statusView];
            [statusView removeFromSuperview];
            for (UIView *view in [UIApplication sharedApplication].keyWindow.subviews) {
                if ([view isKindOfClass:[HFStatusView class]]) {
                    [view removeFromSuperview];
                }
            }
            if (![statusView isInWindow]) {
                
                statusView.statusBarHidden = NO;
                
                if (statusView.statusBlock != nil) {
                    
                    statusView.statusBlock();
                }
            }
        }];
        
    }];
    
    return statusView;
}

#pragma mark - 延迟显示提示弹窗有动画完成回调Block
+(instancetype)showMessageWithTitle:(NSString *)title
                               type:(HFStatusType)type
                    statusBarHidden:(BOOL)hidden
                         afterDelay:(NSTimeInterval)delay
                        finishBlock:(StatusFinishBlock)block
{
    
    HFStatusView *statusView = [[HFStatusView alloc]init];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7     /*延迟执行时间*/ * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        statusView.backgroundColor =   [UIColor whiteColor];
        statusView.x = 0;
        statusView.y =  -hfMessageCountLabelHeight;
        statusView.width = FullScreenWidth;
        statusView.height =  hfMessageCountLabelHeight;
        statusView.type = type;
        statusView.message = title;
        statusView.statusBarHidden = hidden;
        statusView.statusBlock = block;
        
        [[UIApplication sharedApplication].keyWindow addSubview:statusView];
        
        CGFloat duration = 0.32;
        statusView.alpha = 0.0;
        
        [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:.6 initialSpringVelocity:.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            
            statusView.alpha = 1.0;
            
            statusView.transform = CGAffineTransformMakeTranslation(0, hfMessageCountLabelHeight - (HFIphoneX ? 0 : 10));
            
        } completion:^(BOOL finished) {
            
            CGFloat delay = 1.0;
            
            [[UIApplication sharedApplication].keyWindow bringSubviewToFront:statusView];
            
            [UIView animateWithDuration:0.1 delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:^{
                
                statusView.alpha = 0.0;
                
                statusView.transform = CGAffineTransformIdentity;
                
            } completion:^(BOOL finished) {
                
                [[UIApplication sharedApplication].keyWindow bringSubviewToFront:statusView];
                [statusView removeFromSuperview];
                
                if (![statusView isInWindow]) {
                    
                    statusView.statusBarHidden = NO;
                    
                    if (statusView.statusBlock != nil) {
                        
                        statusView.statusBlock();
                    }
                }
            }];
            
        }];
        
        
    });
    
    return statusView;
}

#pragma mark - 隐藏全部提示框
+(void)dismissStatusView
{
    for (HFStatusView *view in [UIApplication sharedApplication].keyWindow.subviews) {
        if ([view isKindOfClass:[HFStatusView class]]) {
            [view removeFromSuperview];
        }
    }
}

+(BOOL)isVisible {
    
    for (HFStatusView *view in [UIApplication sharedApplication].keyWindow.subviews) {
        if ([view isKindOfClass:[HFStatusView class]]) {
            return YES;
        }
    }
    return NO;
}

-(void)setType:(HFStatusType)type
{
    _type =  type;
    _statusImageView.image = [UIImage imageNamed: (self.type == HFStatusType_Success ? @"icon_guanzhuchenggong" : @"icon_wangluoyichang")];
}

-(void)setStatusBarHidden:(BOOL)statusBarHidden {
    _statusBarHidden = statusBarHidden;
    [UIApplication sharedApplication].keyWindow.window.windowLevel  = statusBarHidden ? UIWindowLevelAlert : UIWindowLevelNormal;
}

-(void)setStatusBlock:(StatusFinishBlock)statusBlock {
    _statusBlock = statusBlock;
}

-(void)setMessage:(NSString *)message
{
    _messageLabel.text = message;
    _messageLabel.textColor =  UIColorFromRGB(0x646464);
    
    _messageLabel.font = [UIFont systemFontOfSize:15];
    _messageLabel.width = [self sizeWithString:message font:[UIFont systemFontOfSize:15] width:0 height:21] + 45 > ScreenNarrowSide ?  ScreenNarrowSide - 45 : [self sizeWithString:message font:[UIFont systemFontOfSize:15] width:0 height:21];
    _messageLabel.height = 21;
    _messageLabel.center = CGPointMake(FullScreenWidth / 2 + 12, self.height / 2 + 15);
    _statusImageView.center = CGPointMake(_messageLabel.left - 15, _messageLabel.center.y);
}

-(CGFloat)sizeWithString:(NSString *)str font:(UIFont *)font width:(CGFloat)width height:(CGFloat)height {
    
    CGRect tmpRect = [str boundingRectWithSize:CGSizeMake(width == 0 ? MAXFLOAT : width , height == 0 ? MAXFLOAT : height) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil] context:nil];
    return width == 0 ? tmpRect.size.width : tmpRect.size.height;
}

-(BOOL)isInWindow
{
    for (UIView *view in [UIApplication sharedApplication].keyWindow.subviews) {
        if ([view isKindOfClass:[HFStatusView class]]) {
            return YES;
        }
    }
    return NO;
}
+ (CGAffineTransform)getTransformRotationAngle
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == UIInterfaceOrientationPortrait) {
        return CGAffineTransformIdentity;
    } else if (orientation == UIInterfaceOrientationLandscapeLeft) {
        return CGAffineTransformMakeRotation(-M_PI_2);
    } else if(orientation == UIInterfaceOrientationLandscapeRight) {
        return CGAffineTransformMakeRotation(M_PI_2);
    }
    return CGAffineTransformIdentity;
}
+ (BOOL)isScreenOrientationRight
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if(orientation == UIInterfaceOrientationLandscapeRight) {
        return YES;
    }
    return NO;
}
+ (BOOL)isScreenOrientationLeft
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if(orientation == UIInterfaceOrientationLandscapeLeft) {
        return YES;
    }
    return NO;
}
@end

